<?php
namespace Api\Service;

/**
 * 用户
 */
class UserService
{
    public function register($mobile, $password)
    {
        $mobile_exists = M('user')
            ->where(['mobile' => $mobile])
            ->count();

        if ($mobile_exists) {
            return false;
        }

        $salt = rand_number(0, 9999);
        $data = [
            'username' => $mobile,
            'mobile'   => $mobile,
            'salt'     => $salt,
            'password' => user_password($password, $salt),
            'add_time' => CUR_DATETIME,
        ];
        $id = M('user')->add($data);

        if ($id) {
            $this->register_after($id, $mobile);
        }
        return $id;
    }

    /**
     * 用户注册后的操作
     * @param  [type] $user_id [description]
     * @return [type]          [description]
     */
    protected function register_after($user_id, $username)
    {
        // 注册im
        $this->im($user_id, $username);
    }

    /**
     * 重置密码
     * @param  [type] $mobile   [description]
     * @param  [type] $password [description]
     * @return [type]           [description]
     */
    public function resetPassword($mobile, $password, $old_password = 0)
    {
        $where = [
            'mobile' => $mobile,
        ];

        $user = M('user')
            ->field('id,salt,password')
            ->where($where)
            ->find();

        if (!$user) {
            return false;
        }

        if ($old_password && $user['password'] != user_password($old_password, $user['salt'])) {
            return false;
        }

        $salt = '';
        do {
            $salt = rand_number(0, 9999);
        } while (!($salt && $salt != $user['salt']));

        $data = [
            'salt'     => $salt,
            'password' => user_password($password, $salt),
        ];

        $is_update = M('user')
            ->where([
                'mobile' => $mobile,
            ])->save($data);

        return $is_update;
    }

    /**
     * 用户是否存在
     * @param  string  $mobile [description]
     * @return boolean        [description]
     */
    public function isExists($mobile)
    {
        $user = M('user')
            ->where(['mobile' => $mobile])
            ->count();
        if ($user) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 登录
     * @param  string $value [description]
     * @return [type]        [description]
     */
    public function login($mobile, $password, $ext)
    {
        $where = [
            'mobile' => $mobile,
        ];
        $user = M('user')
            ->field('id,mobile,password,salt')
            ->where($where)
            ->find();
        if (!$user) {
            return false;
        }
        $user_id = $user['id'];

        if (user_password($password, $user['salt']) != $user['password']) {
            return false;
        }

        // 重要 一次token可使用30天
        $data = [
            'id'          => $user_id,
            'expire'      => CUR_TIMESTAMP + 3600 * 24 * 30,
            'device_type' => DEVICE,
            'uuid'        => UUID,
            'version'     => VERSION,
        ];

        // 缓存用户信息
        S('user_token_' . $user_id, $data);
        return $user_id;
    }

    /**
     * 用户信息
     * @param  [type] $user_id [description]
     * @return [type]          [description]
     */
    public function info($user_id)
    {
        if (!$user_id) {
            return [];
        }

        $info            = D('user')->info($user_id);
        $info['collect'] = '5';
        $info['follow']  = '4';

        return $info;
    }

    /**
     * 更新用户信息
     * @param  string $value [description]
     * @return [type]        [description]
     */
    public function updateInfo($user_id, $data)
    {
        $res = M('user')->where([
            'id' => $user_id,
        ])->save($data);

        return $res;
    }

    /**
     * 用户系统Im注册
     * @param  string $user_id  [description]
     * @param  [type] $username [description]
     * @return [type]           [description]
     */
    public function im($user_id = '', $username)
    {
        Vendor('Easemob.Easemob');
        $Easemob = new \Easemob(C('Easemob'));
        $res     = $Easemob->createUser($username, $password = $this->generate_im_password($user_id, $username));

        $data = [
            'user_id'  => $user_id,
            'username' => $username,
            'password' => $password,
            'add_time' => CUR_DATETIME,
        ];
        return M('user_im')->add($data);
    }

    /**
     * 生成im用户密码
     * @param  [type] $user_id  [description]
     * @param  [type] $username [description]
     * @return [type]           [description]
     */
    protected function generate_im_password($user_id, $username)
    {
        return md5(md5($user_id) . $username);
    }
}
